package com.atguigu.gulimall.ssoclient1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;

@Controller
public class Client1LoginController {
    @GetMapping("/employee")
    public String login(@RequestParam(value = "token",required = false) String token, HttpSession session){
        if(!StringUtils.isEmpty(token)){
            RestTemplate restTemplate = new RestTemplate();
            String user = restTemplate.getForObject("http://localhost:8085/userinfo?token="+token, String.class);
            session.setAttribute("loginUser",user);
        }
        Object user = session.getAttribute("loginUser");
        if(user!=null){
            return "employee";
        }
        else {
            //未登录，重定向到认证服务器进行登录
            return "redirect:http://localhost:8085/login?redirect_url=http://localhost:8087/employee";
        }
    }
}
